import { Controller, Get, Inject, Logger, Query, UseFilters, UseInterceptors, Version, VERSION_NEUTRAL } from '@nestjs/common';
import { AppService } from './app.service';
import { ConfigService } from '@nestjs/config';
import { HttpFilter } from './common/filter/http-exception.filter';
import { InjectRedis } from '@nestjs-modules/ioredis';
import Redis from 'ioredis';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { MailerService } from '@nestjs-modules/mailer';
import { PrismaService } from './database/prisma/prisma.service';
import { UserRepository } from './examples/user/user.repository';
import { PrismaClient } from '@prisma/client';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from './examples/user/entities/user.entity';

@Controller({
  // version: ['1', '2'],// 控制器设置版本
  version: VERSION_NEUTRAL, // 某些控制器或路由可能不关心版本，无论版本如何都具有相同的功能
})
@UseFilters(new HttpFilter()) // 将异常过滤器设置在控制器作用域
export class AppController {
  // 使用logger日志
  // private logger = new Logger(AppController.name)

  constructor(
    private readonly appService: AppService,  // 使用主应用服务
    // private readonly configService: ConfigService,   // 使用配置服务
    private readonly mailerService: MailerService,    // 使用邮箱服务
    private readonly userRepostitory: UserRepository,  // 使用统一的repository服务
    // private readonly prismaService: PrismaService, // 使用官方的prisma服务
    @InjectRedis() private readonly redis: Redis,   // 使用redis
    @Inject(CACHE_MANAGER) private readonly cacheManager: Cache,      // 使用cacheManager服务
    @Inject('prisma1') private readonly prismaService: PrismaClient,  // 使用自定义的prisma服务
    @InjectModel('User') private readonly userModel: Model<User>,   // 使用mongoose服务
  ) { }

  @Get('Repos')
  async getHelloRepos(): Promise<any> {
    const user = this.userRepostitory.getRepository()
    const res = await user.find()
    return res
  }

  @Get('mongoose')
  async getHelloMongoose(): Promise<any> {
    const res = await this.userModel.find()
    return res
  }

  @Get('prisma')
  async getHelloPrisma(): Promise<any> {
    const res = await this.prismaService.user.findMany({})
    return res
  }

  @Get('mail')
  async sendMail(): Promise<any> {
    await this.mailerService.sendMail({
      to: 'to@qq.com',
      from: 'from@qq.com',
      subject: '测试模板',
      template: 'Welcome',
      context: {
        name: 'Me'
      }
    })
    return '发送成功'
  }


  @Get()
  @Version('1') // 设置接口版本
  getHello(): string {
    return this.appService.getHello();
  }

  @Get()
  @Version('2') // 设置接口版本
  async getHellov2(@Query('token') token): Promise<any> {
    // 特定键手动指定 TTL（过期时间，以毫秒为单位)，要禁用缓存过期，请将 ttl 配置属性设为 0，可以在CacheModule.register中全局指定
    await this.cacheManager?.set('token', token || 'default token', 50000)
    const res = await this.cacheManager?.get('token')

    return {
      hello: 'v2',
      token: res
    };
  }

  @Get('/helloRedis')
  async getHelloRedis(@Query('token') token): Promise<any> {
    const res = await this.redis?.get('token')
    // 设置值并添加缓存时间
    await this.redis?.set('token', res ? token : 'default token', 'EX', 60 * 10)
    return {
      token: res
    };
  }
}
